// StatusWindow.xaml.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;

using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;

using Supremacy.Client.Events;
using Supremacy.Data;
using Supremacy.Game;
using Supremacy.Messages;
using Supremacy.Messaging;

namespace Supremacy.Client
{
    /// <summary>
    /// Interaction logic for StatusWindow.xaml
    /// </summary>
    public partial class StatusWindow
    {
        #region Fields
        private readonly Table _turnStrings;
        #endregion

        #region Constructors
        public StatusWindow()
        {
            InitializeComponent();

            try
            {
                TableMap enumTables = TableMap.ReadFromFile(
                    "Resources\\Tables\\EnumStrings.txt");
                if (enumTables != null)
                {
                    _turnStrings = enumTables["TurnPhase"];
                }
            }
            catch
            {
                _turnStrings = null;
            }

            Channel<TurnProgressChangedMessage>.Public.Subscribe(
                onNext: o => ProcessTurnPhaseChange(o.TurnPhase),
                threadOption: ChannelThreadOption.UIThread);

            //ClientEvents.TurnPhaseChanged.Subscribe(OnTurnPhaseChanged, ThreadOption.UIThread);
            ClientEvents.TurnStarted.Subscribe(OnTurnStarted, ThreadOption.UIThread);
            ClientEvents.TurnEnded.Subscribe(OnTurnEnded, ThreadOption.UIThread);
            ClientEvents.GameStarted.Subscribe(OnGameStarted, ThreadOption.UIThread);
            ClientEvents.GameEnded.Subscribe(OnGameEnded, ThreadOption.UIThread);
            ClientEvents.ClientDisconnected.Subscribe(OnClientDisconnected, ThreadOption.UIThread);
        }
        #endregion

        #region Methods
        private void OnClientDisconnected(EventArgs t)
        {
            if (IsOpen)
                Close();
        }

        private void OnGameEnded(EventArgs t)
        {
            if (IsOpen)
                Close();
        }

        private void OnGameStarted(DataEventArgs<GameStartData> t)
        {
            ProcessTurnPhaseChange(TurnPhase.WaitOnPlayers);
        }

        private void OnTurnStarted(EventArgs t)
        {
            if (IsOpen)
                Close();
        }

        private void OnTurnEnded(EventArgs t)
        {
            if (IsActive)
                return;
            if (!IsOpen)
                Show();
            Activate();
        }

/*
        private void OnTurnPhaseChanged(DataEventArgs<TurnPhase> t)
        {
            ProcessTurnPhaseChange(t.Value);
        }
*/

        private void ProcessTurnPhaseChange(TurnPhase phase)
        {
            Header = "Processing Turn";

            if ((_turnStrings != null) && (_turnStrings[phase.ToString()] != null))
            {
                Content = _turnStrings[phase.ToString()][0] + "...";
            }
            else
            {
                Content = phase + "...";
            }
        }
        #endregion
    }
}